﻿using Beway.ValueAdded.Model;
using Beway.ValueAdded.Model.ViewModel;
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Beway.ValueAdded.Service
{
    public class ReUserFinanceSvc
    {
        RechargeServ rechSvc = new RechargeServ();
        public List<ReUserFinanceViewModel> PagedList(int page, int rows, int userId,int adminId, out int rcount)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                var q = from f in db.ReUserFinance
                        join u in db.ReUser on f.ReUserId equals u.UserID
                        join a in db.AdminUser on f.AdminId equals a.AdminID
                        select new ReUserFinanceViewModel
                        {
                            AddTime = f.AddTime,
                            AdminId = u.AdminId,
                            AdminUser = a.AdminUserName+(a.TrueName==""?" ":"("+a.TrueName+")"),
                            Amount = f.Amount,
                            Balance = f.Balance,
                            FId = f.FId,
                            Remark = f.Remark,
                            ReUserId = f.ReUserId,
                            ReUserTitle = u.UserTitle,
                            Total = f.Total
                        };

                if (userId>0)
                {
                    q = q.Where(m => m.ReUserId == userId);
                }
                if (adminId > 0)
                {
                    q = q.Where(m => m.AdminId == adminId);
                }
                q = q.OrderByDescending(m => m.FId);

                rcount = q.Count();
                var adm = q.Skip((page - 1) * rows).Take(rows).ToList();
                return adm;
            }
        }

        /// <summary>
        /// 新增财务记录（即给项目增加充值预算），同时增加ReUser表中的RechAmount
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(ReUserFinance model)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                //增加ReUser中的RechAmount总额
                var reUser = db.ReUser.FirstOrDefault(m => m.UserID == model.ReUserId);
                if (reUser != null)
                {
                    decimal total = 0m;
                    decimal balance = 0m;
                    balance = (reUser.RechStock ?? 0) + model.Amount.Value;

                    reUser.RechAmount = (reUser.RechAmount ?? 0) + model.Amount;
                    reUser.RechStock = balance;//修正余额
                 
                    var lastAdd = db.ReUserFinance.Where(m=>m.ReUserId == model.ReUserId).OrderByDescending(m => m.FId).FirstOrDefault();
                    if (lastAdd != null)
                    {
                        total = lastAdd.Total.Value + model.Amount.Value;                      
                    }
                    else
                    {
                        total = model.Amount.Value;                        
                    }               

                    model.Total = total;
                    model.Balance = balance;

                    //新增记录
                    db.ReUserFinance.Add(model);

                    //保存操作
                    db.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }        
    }
}
